﻿Option Strict On

Imports System.Data.SQLite
Imports Sunts.DataLayer.SQLite

Imports Sunts.HyperV.Common

Public Class BackupSchedule

#Region "Properties"
    Public Property BackupJob As BackupJob
    Public Property BackupJobSchedule As BackupJobSchedule
    Public Property BackupDevice As BackupDevice
    Public Property VirtualMachine As VirtualMachine
    Public Property HyperVHost As HyperVHost
    Public Property BackupFolder As String
#End Region

    Public Shared Function Create(ConnectionString As String) As BackupSchedule
        Dim Reader As SQLiteDataReader
        Dim BackupSchedule As BackupSchedule


        Try
            BackupSchedule = New BackupSchedule
            BackupSchedule.BackupJob = New BackupJob
            BackupSchedule.BackupJobSchedule = New BackupJobSchedule
            BackupSchedule.BackupDevice = New BackupDevice
            BackupSchedule.VirtualMachine = New VirtualMachine
            BackupSchedule.HyperVHost = New HyperVHost

            Using sdl As New SQLiteDataLayer(ConnectionString)
                sdl.CommandText = "SELECT * FROM vwBackupSchedule LIMIT 1"

                Reader = sdl.ExecuteDataReader
                While Reader.Read
                    BackupSchedule.BackupJob = BackupJob.SelectByID(CInt(Reader("BackupJobID")), ConnectionString)
                    BackupSchedule.BackupJobSchedule = BackupJobSchedule.SelectByID(CInt(Reader("BackupScheduleID")), ConnectionString)
                    BackupSchedule.BackupDevice = BackupDevice.SelectByID(CInt(Reader("DeviceID")), ConnectionString)
                    BackupSchedule.VirtualMachine = VirtualMachine.SelectByID(CInt(Reader("VirtualMachineID")), ConnectionString)
                    BackupSchedule.HyperVHost = HyperVHost.SelectByID(CInt(Reader("HostID")), ConnectionString)
                End While
                Reader.Close()
            End Using

            If BackupSchedule.BackupJobSchedule.BackupScheduleID = 0 Then
                Create = Nothing
            Else
                Create = BackupSchedule
            End If

        Catch ex As Exception
            Throw New Exception("BackupSchedule: Create()" + ControlChars.CrLf + ex.Message + " " + ex.Source + ex.StackTrace)
        Finally
            BackupSchedule = Nothing
            Reader = Nothing
        End Try

    End Function

End Class

Public Class BackupScheduleCollection
    Inherits System.Collections.ObjectModel.Collection(Of BackupSchedule)
End Class